package com.wanshi.servlet;

import com.alibaba.fastjson.JSON;
import com.wanshi.dao.UserDao;
import com.wanshi.entity.JsonResult;
import com.wanshi.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ss/delUser")
public class DelServlet extends HttpServlet {

    private UserService userService = new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json;charset=utf-8");

        //1 获取要删除用户的id
        String id = request.getParameter("id");

        //2 删除
        int rows=userService.delUser(id);


        //3 重定向到列表的servlet
        //response.sendRedirect(request.getContextPath()+"/ss/list");
        //ajax返回jsp界面
        JsonResult result=null;
        if (rows==0){
            result = new JsonResult(500,"删除失败");
        }else {
            result = new JsonResult(200,"删除成功");
        }

        response.getWriter().write(JSON.toJSONString(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

